<?php
$this->headTitle()->prepend($this->translate('about_category_description').' - ');
$this->headScript()->appendFile($this->APP_STATIC_SERVER.'/js/jquery.json-2.3.min.js');
?>

<?php try {
?>
    <div id="r-heading">
        <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/category.png'; ?>" alt=""/>
        <span><?php echo $this->translate('task_list_categories'); ?></span>
    </div>
    <div style="width: 100%;">
         <form id="frmActions" action="" method="post">
            <div id="actions">
                <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('task_add_category'); ?>" onclick="location = '<?php echo $this->url(array(), 'category_category_add'); ?>';"/>
                <input type="submit" id="btnDelete" class="button" value="<?php echo $this->translate('category_list_action_delete_selected'); ?>"/>
                <input type="hidden" id="hddIDs" value="" name="ids"/>
            </div>
            <table class="list" cellspacing="0">
                <thead>
                    <tr>
                        <td width="3%">
                            <input type="checkbox" class="chkAll"/>
                        </td>
                        <td width="25%">
                        <?php echo $this->translate('category_name'); ?>
                    </td>
                    <td width="25%">
                        <?php echo $this->translate('category_slug'); ?>
                    </td>
                    <td width="15%">
                        <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                    </td>
                    <td width="15%">
                        <?php echo $this->translate('category_module_name'); ?>
                    </td>
                    <td width="15%">
                        <?php echo $this->translate('category_action'); ?>
                    </td>
                </tr>
            </thead>
            <tbody>
                <?php
                        $count = count($this->categories);
                        if ($count == 0) {
                ?>
                    <tr>
                        <td colspan="4" style="color: #fc0202;">
                        <?php echo $this->translate('category_list_not_found'); ?>
                        </td>
                    </tr>
                <?php
                        } else {
                            $count = count($this->categories);
                            $rightStack = array();
                            for($i = 0; $i < $count; $i++) {
                                while(count($rightStack) > 0 && $rightStack[count($rightStack) - 1] < $this->categories[$i]->rht){
                                    array_pop($rightStack);
                                }
                ?>
                    <tr id="category-<?php echo $this->categories[$i]->id ?>" <?php if ($i == $count - 1) echo 'class="last"'; ?>>
                        <td>
                            <input type="checkbox" class="chkId" value="<?php echo $this->categories[$i]->id; ?>"/>
                        </td>
                        <td><?php echo str_repeat('--', count($rightStack)).$this->categories[$i]->name; ?></td>
                        <td><?php echo $this->categories[$i]->slug; ?></td>
                        <td>
                            <?php if (($translations = $this->translationItems($this->categories[$i])) != null) : ?>
					<?php foreach ($translations as $language => $translation) :?>

						<?php if ($translation == null && $language != $this->categories[$i]->language) : ?>
							<?php if (isset($translations[$this->APP_DEFAULT_LANG])) : ?>
							<a class="a_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->id), 'category_category_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
							<?php else :?>
							<a class="a_helper_action_add" href="<?php echo $this->url(array(), 'category_category_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
							<?php endif; ?>
					
						<?php elseif (($translation == null && $language == $this->categories[$i]->language) || ($translation != null && $translation->id == $this->categories[$i]->id)) : ?>
						<a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $this->categories[$i]->id), 'category_category_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						
						<?php elseif ($translation != null && $translation->id != $this->categories[$i]->id) : ?>
						<a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $translation->id), 'category_category_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
						<?php endif; ?>
					<?php endforeach; ?>
				<?php endif; ?>
                        </td>
                        <td><?php echo $this->categories[$i]->module_name; ?></td>
                        <td>
                        <?php if ($this->allow('edit')) {
                        ?>
                                    <a href="<?php echo $this->url(array('id' => $this->categories[$i]->id), 'category_category_edit'); ?>" title=""><?php echo $this->translate('category_list_action_edit'); ?></a>
                        <?php } ?>
                        <?php if ($this->allow('delete')) {?>
                        | <a class="deleteAction" rel="<?php echo $this->categories[$i]->id ?>" href="#" title=""><?php echo $this->translate('category_list_action_delete'); ?></a>
                        <?php } ?>
                        </td>
                   </tr>
                <?php
                                $rightStack[] = $this->categories[$i]->rht;
                            }
                        }
                ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td >
                                <input type="checkbox" class="chkAll"/>
                            </td>
                            <td >
                        <?php echo $this->translate('category_name'); ?>
                    </td>
                    <td >
                        <?php echo $this->translate('category_slug'); ?>
                    </td>
                    <td >
                        <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('category_module_name'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('category_action'); ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<script type="text/javascript">
    $(document).ready(function(){
         //delete Actin
      $(".deleteAction").click(function(){
        if(confirm("<?php echo $this->translate('category_list_action_delete_confirm')?>")){
            var id = $(this).attr("rel");
            $.ajax({
                type: 'POST',
                url: "<?php echo $this->url(array(), 'category_category_delete'); ?>",
                data: { id: id },
                success: function(response) {
                    if ('RESULT_OK' == response) {
                        $('#category-' + id).fadeOut('slow');
                    }
                }
            });
        }
        return false;
      });
	//xóa
	$("#btnDelete").click(function(){
		var arrChkId = $(".chkId");
		var ids = new Array();
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
                      ids.push(arrChkId.eq(i).val());
			}
		}
            if(ids.length > 0){
			if(confirm("<?php echo $this->translate('category_list_action_delete_many_confirm') ?>")){
				$.ajax({
                            type: 'POST',
                            url: "<?php echo $this->url(array(), 'category_category_delete'); ?>",
                            data: { id: $.toJSON(ids) },
                            success: function(response) {
                                if ('RESULT_OK' == response) {
                                    for(var i in ids)
                                        $('#category-' + ids[i]).fadeOut('slow');
                                }
                            }
                        });
			}else{
				return false;
			}
		}else{
			alert("<?php echo $this->translate('category_list_bulk_action_none_selected')?>");
			return false;
		}
            return false;
	});

    });
</script>
<?php
} catch (Exception $ex) {
    echo $ex->getMessage();
}